/*global jQuery, $, geo_position_js, initialize, goToPage, ResultList, resultMap */
var rateme = rateme || {};

// global
rateme.QYPE_API_KEY = '5FVNdon5fzYzCRGwcPc57g';
rateme.MY_POS = null;

jQuery(document).ready(function () {

	var q = new rateme.qype(rateme.QYPE_API_KEY);
	
	// 1.1 Search By Address
	
	$('#search-place-by-address form').submit(function () {
		var $form = $(this),
			$result = null;
			
		$result = $form.parent().children('.result');
		
		$result.addClass('hide').find('.map').replaceWith('<div class="map"></div>');
		
	    $(q).bind('successByAddress', function (e, data) {
			
			goToPage('#search-place-result', '#search-place-by-address');
			var rl = new ResultList(data, '#search-place-result');
			
	    });
	    
	    q.getPlacesByAddress($form.find('input[name="address"]').val());
		
		return false;
	});
	
	
	// 1.2 Search By Name
	
	$('#search-place-by-name form').submit(function () {
		var $form = $(this),
			$result = null;
		
		$result = $form.parent().children('.result');
		
		$result.addClass('hide').find('.map').replaceWith('<div class="map"></div>');
		
	    $(q).bind('successByName', function (e, data) {
			
			goToPage('#search-place-result', '#search-place-by-name');
			var rl = new ResultList(data, '#search-place-result');
	    });
	    
	    
	    q.getPlacesByName({
			'name' : $form.find('input[name="name"]').val(),
			'city' : $form.find('input[name="city"]').val()
		});
			
		
		return false;
	});
	
	
	// 1.3 Search By Near Me
	
	$('#search-place-near-me form').submit(function () {
		var $form = $(this),
			$result = null,
			g = null;
		
		$result = $form.parent().children('.result');
			
		$result.addClass('hide').find('.map').replaceWith('<div class="map"></div>');
		
		g = new rateme.geo();
		
		$(q).bind('successByLatLng', function (e, data, myPos) {
			
			rateme.MY_POS = myPos;
			
			goToPage('#search-place-result', '#search-place-near-me');
			var rl = new ResultList(data, '#search-place-result');
	    });
	
		$(g).bind('success', function (e, data) {
	
			q.getPlacesByLatLng({
				'lat' : data.lat,
				'lng' : data.lng
			});
		});
			
		g.getPositionLatLng();
				
		return false;
	});
	
	/**
	 * When the result list has completed, bind links to function
	 * @param {Object} e
	 * @param {String} list selector
	 */	
	$('#search-place-result').bind('resultlistcomplete', function (e, data) {
		var pageid = data.pageid;

		$(q).bind('successById', function (e, data) {
			goToPage('#search-place-details', pageid);
			var rd = new rateme.resultdetails(data),
			qypeData = null;
			
			/**
			 * The function resultMap takes "data" as its first parameter. data is the returned object by Qype. In order to be consistent, I reconstruct this data structure.
			 * to resemble data.results[i].place.point
			 */
			qypeData = {
				'results' : [
					{
						'place' : {
							'point' : rd.getPoint()
						}
					}
				]
			};
			
			$('#search-place-details').find('.result').removeClass('hide').html(rd.toString());
	
			$('#search-place-details').find('.map a').click(function () {
				goToPage('#search-place-map', '#search-place-details');
				resultMap(qypeData, '#search-place-map .map');
				$('#search-place-map').find('.result').removeClass('hide');
				return false;
			});
		});

		$(data.pageid).find('.result .list ul li a').click(function () {
			q.getPlaceById($(this).attr('qypeid'));
			return false;
		});
	});
	
});